package response

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

type AjaxResponse struct {
	Code int32       `json:"code"`    // 状态码
	Msg  string      `json:"message"` // 提示信息
	Data interface{} `json:"data"`    // 数据
}

const (
	CodeSuccess          = 200
	CodeError            = 500
	CodeUnauthorized     = 401
	CodeForbidden        = 403
	CodeNotFound         = 404
	CodeMethodNotAllowed = 405

	MsgSuccess = "操作成功"
	MsgError   = "操作失败"
)

func Success(c *gin.Context, data interface{}) {
	response := AjaxResponse{
		Code: CodeSuccess,
		Msg:  MsgSuccess,
		Data: data,
	}
	c.AbortWithStatusJSON(http.StatusOK, response)
}
func Fail(c *gin.Context, msg string) {
	response := AjaxResponse{
		Code: CodeError,
		Msg:  msg,
		Data: nil,
	}
	c.AbortWithStatusJSON(http.StatusOK, response)
}
func SuccessDefault(c *gin.Context) {
	response := AjaxResponse{
		Code: CodeSuccess,
		Msg:  MsgSuccess,
		Data: nil,
	}
	c.AbortWithStatusJSON(http.StatusOK, response)
}

func FailDefault(c *gin.Context) {
	response := AjaxResponse{
		Code: CodeError,
		Msg:  MsgError,
		Data: nil,
	}
	c.AbortWithStatusJSON(http.StatusOK, response)
}

func Resp(c *gin.Context, code int32, msg string, data interface{}) {
	response := AjaxResponse{
		Code: code,
		Msg:  msg,
		Data: data,
	}
	c.AbortWithStatusJSON(http.StatusOK, response)
}

func ToAjax(c *gin.Context, flag bool) {
	if flag {
		SuccessDefault(c)
	} else {
		FailDefault(c)
	}
}
